package frsf.cidisi.faia.tools;

public class Matrix {

	private int nrows;
	private int ncols;
	public double[][] data;

	public Matrix(double[][] dat) {
		this.data = dat;
		this.nrows = dat.length;
		this.ncols = dat[0].length;
	}

	public Matrix(int nrow, int ncol) {
		this.nrows = nrow;
		this.ncols = ncol;
		data = new double[nrow][ncol];
	}

	public int getNrows() {
		return nrows;
	}

	public void setNrows(int nrows) {
		this.nrows = nrows;
	}

	public int getNcols() {
		return ncols;
	}

	public void setNcols(int ncols) {
		this.ncols = ncols;
	}

	public double[][] getValues() {
		return data;
	}

	public void setValues(double[][] values) {
		this.data = values;
	}

	public void setValueAt(int row, int col, double value) {
		data[row][col] = value;
	}

	public double getValueAt(int row, int col) {
		return data[row][col];
	}

	public boolean isSquare() {
		return nrows == ncols;
	}

	public int size() {
		if (isSquare())
			return nrows;
		return -1;
	}

	public Matrix multiplyByConstant(double constant) {
		Matrix mat = new Matrix(nrows, ncols);
		for (int i = 0; i < nrows; i++) {
			for (int j = 0; j < ncols; j++) {
				mat.setValueAt(i, j, data[i][j] * constant);
			}
		}
		return mat;
	}
	public Matrix insertColumnWithValue1() {
		Matrix X_ = new Matrix(this.getNrows(), this.getNcols()+1);
		for (int i=0;i<X_.getNrows();i++) {
			for (int j=0;j<X_.getNcols();j++) {
				if (j==0)
					X_.setValueAt(i, j, 1.0);
				else 
					X_.setValueAt(i, j, this.getValueAt(i, j-1));
				
			}
		}
		return X_;
	}
        
        
        public static Matrix identity(int n){
            Matrix nueva = new Matrix(n,n);
            for (int i=0;i<nueva.getNrows();i++) {
			for (int j=0;j<nueva.getNcols();j++) {
                        if(i==j) nueva.setValueAt(i, j, 1);
                        else nueva.setValueAt(i, j, 0);
                        }}
            return nueva;
        }
        
        public void show(){
            System.out.println();
        for ( int i=0; i < this.getNrows(); i++ ) {
            System.out.print("["); 
            for ( int j=0; j < this.getNcols(); j++ )
             {
             System.out.print(" ");
             System.out.print(data[i][j]);
             }
             System.out.print("]");
            System.out.println();
        }
        }
                
         /*      
        public Object clone(){
        Matrix obj=null;
        try{
            obj=(Matrix)super.clone();
        }catch(CloneNotSupportedException ex){
            System.out.println(" no se puede duplicar");
        }
//aquí está la clave  para clonar la matriz bidimensional
        obj.data=(double[][])obj.data.clone();
        for(int i=0; i<obj.data.length; i++){
            obj.data[i]=(double[])obj.data[i].clone();
        }
        return obj;
    }*/

}
